// prizeDetail.js
const HOST = require('../../config.js').host
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    prizeType:"",
    isShow:"",
    phone_number:"",
    onOff:true,
    zjid:"",
    msg:{}
  },
  onShareAppMessage() {
    let that = this;
    return {
      title: '游西湖，赢好礼',
      path: '/pages/giftGame/giftGame',
      imageUrl:'/assets/images/choujiangfenxiang.png',
      success: function (res) {
        console.log(res)
        console.log(app)
        console.log("记录参数"+app.userId)
        console.log(that.data)
      //记录分享次数
        wx.request({
          url: `${HOST}v1/shareinfo/updateShareInfo`,
          method:"POST",
          data:{
            userId:app.userId
          },
          success:res=>{
            if (res.data.data && (res.data.data.shareTimes >= 30)) {
              wx.hideShareMenu();
            } else {
              wx.reLaunch({
                url: '/pages/giftGame/giftGame?isSharedRederact=true'
              })
            }
          }
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let prizeType = options.prizeType;
    let id = options.id;
    let that = this;
    // let prizeType = 'B'
    let isShow = ''
    // 获取推荐
    wx.request({
      url: `${HOST}v1/product/findRecommendProducts`,
      success:res=>{
        let msg = res.data.data;
        console.log(msg)
        let imgUrl = '';
        for(let i = 0;i<msg.length;i++){
          imgUrl = msg[i].portalImg.split(',')[0]
          msg[i].portalImg = imgUrl
        }
        console.log(imgUrl)
        that.setData({
          msg
        })
      }
    })
    if(prizeType == 'A'){
      isShow=false
    }else{
      isShow=true
    }
    this.setData({
      prizeType: prizeType,
      
      isShow:isShow,
      zjid:id
    })
    // 代码
    //是否提供分享功能
    wx.request({
      url: `${HOST}v1/shareinfo/findByUserId/${app.userId}`,
      method: "GET",
      success: res => {
        console.log("查询参数"+app.userId)
        if (res.data.data && (res.data.data.shareTimes >= 30)) {
          wx.hideShareMenu();
        } else {
          wx.showShareMenu({
            withShareTicket: true
          })
        }
      }
    })
  },
  getMore(){
    wx.switchTab({
      url: '../home/home',
    })
  }
  ,
  goToYouLike(e){
    wx.reLaunch({
      url: `../detail/detail?id=${e.currentTarget.dataset.productid}`,
    })
  }
  ,
  goTicket:()=>{
    wx.request({
      url: `${HOST}v1/product/getProductIds`,
      success:res=>{
        console.log(res.data.data)
        let type = res.data.data
        wx.reLaunch({
          url:`../getTicket/getTicket`,
        })
      }
    })
    
  }
  ,
  submitInfo(e){
    let that = this;
    if(e.detail.value.length == 11){
      that.setData({
        phone_number:e.detail.value,
        onOff:false
      })
    }else{
      wx.showToast({
        title: '请输入正确的手机号',
        image:'../../assets/images/error.png'
      })
    }
  }
  ,
  submit(){
    let that = this
    // 调用短信接口
      wx.request({
        url: HOST + 'v1/sendmessage/' + that.data.phone_number,
        method:'GET',
        success:(res)=>{
          wx.showToast({
            title: '信息提交成功',
            icon: 'success',
            duration: 2000
          })
          that.setData({
            isShow: true
          })
        }
      })
// 存储信息到数据库
      wx.request({
        url: HOST +'v1/winrecord/saveUserMobile',
        method:'POST',
        data:{
          userMobile: that.data.phone_number,
          id:that.data.zjid
        },
        success:(res)=>{
          console.log(res)
          console.log(that.data.zjid)
        }
      })
      
  }
  ,
  checkPrizeHistory() 
  {
    wx.navigateTo({
      url: '../user/prize/prize',
    })

  },
  goHomePage() //进入首页
  {
    wx.switchTab({
      url: '../home/home',
    })
  },
  shoppingDetail()
  {
    let id = "3bf8cd8d-80b1-4378-8339-132f147e";
    wx.navigateTo({
      url: `../store/store?id=${id}`
    })
  }
})